package com.lurencun.android.topicbank.util;

import java.sql.SQLException;
import java.util.List;

import com.lurencun.android.topicbank.presist.DataHelper;
import com.lurencun.android.topicbank.presist.Profile;

import android.content.Context;
import android.util.Log;

public class ProfileUtil {

	private static ProfileUtil instance;
	
	public static ProfileUtil getInstance() {
		if (instance==null) {
			instance = new ProfileUtil();
		}
		return instance;
	}
	
	public Profile hasProfile(Context context) {
		DataHelper dataHelper = new DataHelper(context);
		Profile profile;
		try {
			List<Profile> ls = dataHelper.getProfileDao().queryForAll();
			if (ls!=null && ls.size() > 0) {
				profile= ls.get(0);
				Log.d("MM--->DeviceInfo", "has profile=" + profile);
				return profile;
			}
			else {
				profile = new Profile();
				profile.setAndroidID(DeviceUtil.getAndroidID(context));
				profile.setImei(DeviceUtil.getIMEI(context));
				profile.setDesKey(DESEncrypt.createDESkey());
				dataHelper.getProfileDao().create(profile);
				Log.d("MM--->DeviceInfo", "new profile=" + profile);
				return profile;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
}
